<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 * @property Post_model $post_model
 */
class Export_sim extends MX_Controller
{
    private $_langCode;

    function __construct()
    {
        parent::__construct();
        $this->load->library('Layout');
        $this->_langCode = $this->mycommon->getLangCode();
    }

    public function index($page='',$type='', $order='')
    {
    	//echo $_SERVER['DOCUMENT_ROOT'].'/app/libraries/dompdf/dompdf_config.inc.php'; die;
    	
    	$data = array();
    	if(isset($_GET["page"])) {
    		$page = $_GET["page"];
    	}
        if(isset($_GET["type"])) {
            $type = $_GET["type"];
        }
        if(isset($_GET["order"])) {
            $order = $_GET["order"];
        }
        
    	$page = (intval($page) != 0) ? intval($page) : 1;
    	
    	$this->load->model('option/option_model');
    	$this->load->model('category/category_model');
    	$this->load->model('post/post_model');
    	$this->load->model('list_network/list_network_model');
    	$this->load->model('item_sim/item_sim_model');
    	$this->load->model('list_group/list_group_model');
    	
    	$tmpInfo = $this->list_network_model->getListNetwork();
    	$listGroup = $this->list_group_model->getListGroup();
    	$per_page = 50;
    	
    	$start = ($page - 1) * $per_page;
    	
    	//$tmpPost = $this->post_model->getPostByCatId($id, $config['per_page'], $start, $langCode);

    	$post = $this->item_sim_model->getAllByType($per_page, $start, $type, $order);
    	
    	$data['list_network'] = $tmpInfo;
    	$data['list_group'] = $listGroup;
    	$data['post'] = $post;
    	
    	$html = $this->load->view('export/export_view', $data,true);
    	$html = mb_convert_encoding($html, "iso-8859-2", "utf8");
    	
    	$path = getcwd()."/dompdf/dompdf_config.inc.php";
    	require_once($path);
    	spl_autoload_register('DOMPDF_autoload');
    	$dompdf = new DOMPDF();
    	$dompdf->load_html($html);
    	$dompdf->render();
    	$dompdf->stream("my_pdf.pdf", array("Attachment" => 0));
    }
     public function search()
    {
        //echo $_SERVER['DOCUMENT_ROOT'].'/app/libraries/dompdf/dompdf_config.inc.php'; die;
        
        $data = array();
        //mang?
        if(isset($_GET["snetwok"])) {
            $networkId = $_GET["snetwok"];
        }
        //nho'm nao?
        if(isset($_GET["sgroup"])) {
            $groupId = $_GET["sgroup"];
        }
        //gia 0.5-1 0.5: 500k -> 1tr
        if(isset($_GET["gia"])) {
            $gia = $_GET["gia"];
        }
        //10 so hay 11 so
        
        if(isset($_GET['text_sim'])) {
            $textsim = $_GET['text_sim'];
        }
        if(isset($_GET["type"])) {
            $type = $_GET["type"];
        }
        if(isset($_GET["order"])) {
            $order = $_GET["order"];
        }
        if(isset($_GET["page"])) {
            $page = $_GET["page"];
        }

        if(!isset($networkId)) $networkId = 0;
        if(!isset($groupId)) $groupId = 0;
        if(!isset($gia)) $gia = 0;
        if(!isset($dauso) || $dauso == 0) $dauso = '%';
        if(!isset($dau) || $dau == 0) $dau = 0;
        if(!isset($order) || $order == 0) $order = 1;
        if(!isset($page) || $page == 0) $page = 1;
        if($order == 1) {
            $order = 'ASC';
        } else {
            $order = 'DESC';
        }
        //echo $dauso;
        
        $giaArray = explode('-', $gia);
        $startGia = 0;
        $endGia = 5000;
        if(count($giaArray) ==2) {
            $startGia = $giaArray[0];
            $endGia = $giaArray[1];
        }
        $startGia = $startGia * 1000000;
        $endGia = $endGia * 1000000;
        
        if(!isset($textsim) || $textsim == '') $textsim = '*';
        $textSim = $textsim;
        $textsim = str_replace('*', '%', $textsim);
        $start = ($page - 1) * 50;
        
        $this->load->model('item_sim/item_sim_model');
        $tmpPost = $this->item_sim_model->searchArray($textsim, $networkId,$groupId, 50, $startGia, $endGia, $dauso, $type, $order, $start);

        
        $data['post'] = $tmpPost;
        
        $html = $this->load->view('export/export_view', $data,true);
        $html = mb_convert_encoding($html, "iso-8859-2", "utf8");
        
        $path = getcwd()."/dompdf/dompdf_config.inc.php";
        require_once($path);
        spl_autoload_register('DOMPDF_autoload');
        $dompdf = new DOMPDF();
        $dompdf->load_html($html);
        $dompdf->render();
        $dompdf->stream("my_pdf.pdf", array("Attachment" => 0));
    }
    
    public function change()
    {
        $data = array();
    	$this->load->model('option/option_model');
    	$this->load->model('category/category_model');
    	$this->load->model('post/post_model');
    	$this->load->model('list_network/list_network_model');
    	$this->load->model('item_sim/item_sim_model');
    	$this->load->model('list_group/list_group_model');
    
    	$tmpInfo = $this->list_network_model->getListNetwork();
    	$listGroup = $this->list_group_model->getListGroup();
    
    	$data['list_network'] = $tmpInfo;
    	$data['list_group'] = $listGroup;
    
    	$this->layout->view('landing/change_view', $data);
    }
    public function support()
    {
    	$data = array();
    	$this->load->model('option/option_model');
    	$this->load->model('category/category_model');
    	$this->load->model('post/post_model');
    	$this->load->model('list_network/list_network_model');
    	$this->load->model('item_sim/item_sim_model');
    	$this->load->model('list_group/list_group_model');
    
    	$tmpInfo = $this->list_network_model->getListNetwork();
    	$listGroup = $this->list_group_model->getListGroup();
    
    	$data['list_network'] = $tmpInfo;
    	$data['list_group'] = $listGroup;
    
    	$this->layout->view('landing/support_view', $data);
    }
    public function about()
    {
    	$data = array();
    	$this->load->model('option/option_model');
    	$this->load->model('category/category_model');
    	$this->load->model('post/post_model');
    	$this->load->model('list_network/list_network_model');
    	$this->load->model('item_sim/item_sim_model');
    	$this->load->model('list_group/list_group_model');
    
    	$tmpInfo = $this->list_network_model->getListNetwork();
    	$listGroup = $this->list_group_model->getListGroup();
    
    	$data['list_network'] = $tmpInfo;
    	$data['list_group'] = $listGroup;
    
    	$this->layout->view('landing/about_view', $data);
    }
    public function sales()
    {
    	$data = array();
    	$this->load->model('option/option_model');
    	$this->load->model('category/category_model');
    	$this->load->model('post/post_model');
    	$this->load->model('list_network/list_network_model');
    	$this->load->model('item_sim/item_sim_model');
    	$this->load->model('list_group/list_group_model');
    
    	$tmpInfo = $this->list_network_model->getListNetwork();
    	$listGroup = $this->list_group_model->getListGroup();
    
    	$data['list_network'] = $tmpInfo;
    	$data['list_group'] = $listGroup;
    
    	$this->layout->view('landing/sales_view', $data);
    }
    public function contact()
    {
    	$data = array();
    	$this->load->model('option/option_model');
    	$this->load->model('category/category_model');
    	$this->load->model('post/post_model');
    	$this->load->model('list_network/list_network_model');
    	$this->load->model('item_sim/item_sim_model');
    	$this->load->model('list_group/list_group_model');
    
    	$tmpInfo = $this->list_network_model->getListNetwork();
    	$listGroup = $this->list_group_model->getListGroup();
    
    	$data['list_network'] = $tmpInfo;
    	$data['list_group'] = $listGroup;
    
    	$this->layout->view('landing/contact_view', $data);
    }
    //add sim save session
    public function addCard()
    {
    	session_start();
    	//session_unset();
    	$data = array();
    	$data['sim'] = array();
    	$data['gia'] = array();
    	if(isset($_SESSION['sim'])) {
    		$data['sim'] = $_SESSION['sim'];
    	}
    	if(isset($_SESSION['gia'])) {
    		$data['gia'] = $_SESSION['gia'];
    	}

    	if(isset($_GET['sim'])) {
    		$sim = $_GET['sim'];
    	}
    	if(isset($_GET['gia'])) {
    		$gia = $_GET['gia'];
    	}

    	if(count($data['sim'])>0) {
    		if(array_search($sim, $data['sim']) != NULL){
	    		$data['sim'][] = $sim;
	    		$data['gia'][] = $gia;
	    		session_unset();
	    		$_SESSION['sim']=$data['sim'];
	    		$_SESSION['gia']=$data['gia'];

    		} 
    			
    	}
    	//print_r($data);
    	$html = $this->load->view('landing/add_card_view', $data,true);
    	$return['status'] = 1;
    	$return['html'] = $html;

    	echo json_encode($return); exit;

    }

}
?>
